//
//  MenstruatDateViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/28.
//

#import "MenstruatDateViewController.h"
#import "CustomDateViewController.h"

@interface MenstruatDateViewController ()
@property(nonatomic,strong) NSMutableArray *zhouqiArr;
@property(nonatomic,strong) NSMutableArray *jingqiArr;

@property(nonatomic,strong) QMUITextField *zhouqiTf;
@property(nonatomic,strong) QMUITextField *jingqiTf;
@property(nonatomic,strong) QMUITextField *yuejingTf;
@end

@implementation MenstruatDateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"月经期";
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.zhouqiArr = @[].mutableCopy;
    self.jingqiArr = @[].mutableCopy;
    // 周期 24 ~ 35
    for (int i = 24; i < 36; i++) {
        [self.zhouqiArr addObject:FormatString(@"%d天",i)];
    }
    // 经期 4 ~ 8
    for (int i = 4; i < 9; i++) {
        [self.jingqiArr addObject:FormatString(@"%d天",i)];
    }
}

- (void)initSubviews {
    [super initSubviews];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myHorzMargin = 15;
    root.myTop = 15 + NavigationContentTop;
    root.myHeight = kAutoHeight;
    [self.view addSubview:root];
    
    MyLinearLayout *contentLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    contentLay.myHorzMargin = 0;
    contentLay.padding = UIEdgeInsetsMake(0, 15, 0, 15);
    contentLay.backgroundColor = COLOR_4;
    contentLay.layer.cornerRadius = 15;
    contentLay.myHeight = kAutoHeight;
    [root addSubview:contentLay];
    
    NSArray *array = @[@"周期长度",@"经期长度",@"上次月经",@"经期开始提醒",@"设备提醒",@"提醒时间"];
    for (int i = 0; i < array.count; i++) {
        MyLinearLayout *itemLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        itemLay.myHorzMargin = 0;
        itemLay.gravity = MyGravity_Vert_Center;
        itemLay.myHeight = 52;
        itemLay.tag = 10 + i;
        [contentLay addSubview:itemLay];
        [itemLay setTarget:self action:@selector(itemLayClick:)];
        
        if(i < array.count - 1)[contentLay addSubview:[self setAddLineTop:0]];
        
        {
            
            UILabel *name = [UILabel createLabelText:array[i] font:UIFontMake(15) textColor:COLOR1515];
            name.mySize = kAutoSize;
            if(i == 4){
                name.weight = 1;
            }
            [itemLay addSubview:name];
            
            if(i == 4){
                UISwitch *sw = [[UISwitch alloc]init];
                sw.mySize = kAutoSize;
                sw.on = YES;
                sw.onTintColor = THEME_COLOR;
                [itemLay addSubview:sw];
            }else{
                QMUITextField *tf = QMUITextField.new;
                tf.myHeight = kAutoHeight;
                tf.weight = 1;
                tf.enabled = NO;
                tf.textColor = HexColor(@"#454545");
                tf.font = UIFontMake(15);
                tf.textAlignment = NSTextAlignmentRight;
                NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"请选择" attributes:
                                                             @{NSForegroundColorAttributeName:HexColor(@"#BABABA"),
                                                               NSFontAttributeName:UIFontMake(15)}
                ];
                tf.attributedPlaceholder = attrString;
                [itemLay addSubview:tf];
                if(i == 0){
                    self.zhouqiTf = tf;
                }else if (i == 1){
                    self.jingqiTf = tf;
                }else if (i == 2){
                    self.yuejingTf = tf;
                }
                
                UIImageView *right = [[UIImageView alloc]initWithImage:UIImageMake(@"icon_arrow_right")];
                right.mySize = CGSizeMake(16, 16);
                [itemLay addSubview:right];
            }
            
            
        }
    
    }
    
    
    UIButton *submitBtn = [UIButton createButtonText:@"开始记月经模式" font:UIFontMake(14) textColor:COLOR_4];
    submitBtn.myHorzMargin = 0;
    submitBtn.myTop = 30;
    submitBtn.myHeight = 46;
    ViewCorner(submitBtn, 23);
    [submitBtn az_setGradientBackgroundWithColors:@[HexColor(@"#4BD8B1"),HexColor(@"#2CAED8")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
    [submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [root addSubview:submitBtn];
}

- (void)itemLayClick:(UIView *)view {
    NSInteger tag = view.tag - 10;
    WeakSelf
    switch (tag) {
        case 0:
        {
            [BRStringPickerView showPickerWithTitle:@"请选择周期长度" dataSourceArr:self.zhouqiArr selectIndex:5 resultBlock:^(BRResultModel * _Nullable resultModel) {
                weakSelf.zhouqiTf.text = resultModel.value;
            }];
        }
            break;
        case 1:
        {
            [BRStringPickerView showPickerWithTitle:@"请选择周期长度" dataSourceArr:self.jingqiArr selectIndex:2 resultBlock:^(BRResultModel * _Nullable resultModel) {
                weakSelf.jingqiTf.text = resultModel.value;
            }];
        }
            break;
        case 2:
        {
            NSDate *min = [NSDate dateWithTimeInterval:-3600*24*50 sinceDate:[NSDate date]];
            [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:@"上次月经" selectValue:[self getCurrentDate] minDate:min maxDate:[NSDate date] isAutoSelect:YES resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
                weakSelf.yuejingTf.text = selectValue;
            }];
        }
            break;
        default:
            break;
    }
    
    
}

- (void)submitBtnClick {
    if(self.zhouqiTf.text.length == 0){
        [QMUITips showError:@"请选择周期长度" inView:self.view];
        return;
    }
    if(self.jingqiTf.text.length == 0){
        [QMUITips showError:@"请选择经期长度" inView:self.view];
        return;
    }
    if(self.yuejingTf.text.length == 0){
        [QMUITips showError:@"请选择上次月经时间" inView:self.view];
        return;
    }
    
    CustomDateViewController *vc = CustomDateViewController.new;
    vc.periodLength = [self.zhouqiTf.text substringToIndex:self.zhouqiTf.text.length - 1].integerValue;
    vc.menstrualPeriod = [self.jingqiTf.text substringToIndex:self.jingqiTf.text.length - 1].integerValue;
    vc.periodStartTime = self.yuejingTf.text;
    vc.title = self.title;
    [self showViewController:vc sender:self];
}

- (NSString *)getCurrentDate {
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    format.dateFormat = @"yyyy-MM-dd";
    NSString *string = [format stringFromDate:[NSDate date]];
    return string;
}

@end
